<script>
import { HTTP_REQUEST_URL, SYSTEM_VERSION } from './config/app';
import { getSystemInfo, getVersion } from './api/system';
import { silenceAuth, basicConfig } from './api/public';
import { VersionUpdate } from './utils/VersionUpdate';
import Routine from './libs/routine.js';
export default {
  globalData: {
    name: 'sccc',
  },
  data() {
    return {
      calcRight: 0,
      calcPtTop: 0,
      customHeaderTop: 0,
      userAvatar: ""
    }
  },
  onLaunch(options) {
     console.log('---当前正式小程序版本号:', uni.getAccountInfoSync().miniProgram);
    // this.checkVersion()
    // console.log("获取小程序码的参数",options.query); // 获取小程序码的参数
    uni.hideTabBar({
      animation: false,
    });
    basicConfig().then((res) => {
      uni.setStorageSync('BASIC_CONFIG', res.data);
    });

    let that = this;
    // #ifdef MP
    if (HTTP_REQUEST_URL == '') {
      console.error(
        "请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
      );
      return false;
    }
    // 获取系统信息
    uni.getSystemInfo({
      success(res) {
        getSystemInfo(res)
        that.codeheight = Math.round(res.screenHeight);
        that.phoneModel = res.model
      }
    });
    VersionUpdate()

    /*const updateManager = wx.getUpdateManager();
    console.log('updateManager')
    console.log(updateManager);
    const startParamObj = wx.getEnterOptionsSync();
    console.log('startParamObj',startParamObj)
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager();
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log("请求完新版本信息的回调")
        console.log(res.hasUpdate)
        if (res.hasUpdate) {
          updateManager.onUpdateFailed(function () {
            return that.Tips({
              title: '新版本下载失败'
            });
          });
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经下载好，是否重启当前应用？',
              success(res) {
                if (res.confirm) {
                  updateManager.applyUpdate();
                }
              }
            });
          });
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '发现新版本',
              content: '请删除当前小程序，重启搜索打开...'
            });
          });
        }
      });
    }*/
    // #endif
    // 获取导航高度；
    uni.getSystemInfo({
      success: function (res) {
        // console.log("高度")
        // console.log(res)
        // that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
      }
    });
    // #ifdef MP
    let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
    that.globalData.navH = menuButtonInfo.top * 2 + menuButtonInfo.height / 2;
    const version = uni.getSystemInfoSync().SDKVersion;
    if (Routine.compareVersion(version, '2.21.3') >= 0) {
      that.$Cache.set('MP_VERSION_ISNEW', true);
    } else {
      that.$Cache.set('MP_VERSION_ISNEW', false);
    }
    // #endif

    // #ifdef MP

    //小程序静默授权
    if (!this.$store.getters.isLogin) {
      Routine.getCode()
        .then(code => {
          this.silenceAuth(code);
        })
        .catch(res => {
          uni.hideLoading();
        });
    }
    // #endif
  },
  onShow: function () {
    // console.log('App Show')
  },
  onHide: function () {
    // console.log('App Hide')
  },
  methods: {
    checkVersion() {
      // 获取本地版本
      let devVersion = ''
      let version = ''
      // console.log('=======',this.compareVersion(version, devVersion))
      let accountInfo = uni.getAccountInfoSync().miniProgram; // 获取当前小程序版本号
      if (accountInfo.envVersion === 'release') {
        console.log('---当前正式小程序版本号:', accountInfo.version);
        devVersion = accountInfo.version
      }
      // 测试使用
      if (accountInfo.envVersion === 'trial') {
        console.log('---当前c测试小程序版本号:', accountInfo.version);
      }
      // 获取到的版本号
      getVersion().then(res => {
        console.log('---当前小程序线上获取到版本号:', );
        version = res.data.version
      })
      // getVersion().then(res => {
      // })
      if (this.compareVersion(version, devVersion) < 0) {
        uni.showModal({
          title: '版本过低',
          content: '当前版本过低，请更新到最新版本后使用',
          showCancel: true,
          cancelText: '取消',
          confirmText: '更新',
          success: (res) => {
            if (res.confirm) {
              // 引导用户更新，例如打开小程序的更新页面或跳转到应用市场
             uni.reLaunch({ url: '/pages/index/home' }); // 强制重启小程序到首页
            } else if (res.cancel) {
              // 用户选择不更新，这里可以选择重启小程序或者阻止继续使用当前版本的功能
              uni.reLaunch({ url: '/pages/index/home' }); // 强制重启小程序到首页
            }
          }
        });
      }
    },
    compareVersion(v1, v2) {
      v1 = v1.split('.');
      v2 = v2.split('.');
      const k = Math.min(v1.length, v2.length);
      for (let i = 0; i < k; ++i) {
        v1[i] = parseInt(v1[i], 10);
        v2[i] = parseInt(v2[i], 10);
        if (v1[i] > v2[i]) return 1;
        if (v1[i] < v2[i]) return -1;
      }
      return v1.length > v2.length ? 1 : v1.length < v2.length ? -1 : 0;
    },
    // 小程序静默授权
    silenceAuth(code) {
      let that = this;
      let spread = that.globalData.spid ? that.globalData.spid : '';
      silenceAuth({
        code: code,
        spread_spid: spread,
        // spread_code: that.globalData.code
      })
        .then(res => {
          let openid = res.data.openid
          that.$Cache.set('OPENID', openid);
        })
        .catch(res => { });
    },
  }
}
</script>

<style lang="scss">
/*每个页面公共css */
@import './uni.scss';
@import './common/css/public.scss';



// scroll-view 隐藏滚动条
::-webkit-scrollbar {
  display: none;
  width: 0 !important;
  height: 0 !important;
  -webkit-appearance: none;
  background: transparent;
}

// .uni-tabbar__item-icon {
// 	width: 30rpx;
// 	height: 30rpx;
// }

// padding-top: env(safe-area-inset-top);
// padding-left: env(safe-area-inset-left);
// padding-right: env(safe-area-inset-right);
// padding-bottom: env(safe-area-inset-bottom);</style>
